<?php

/**
 * Class Logger
 */
class Logger
{
    /**
     * @var string
     */
    protected static $fname = './places_<date>.log';

    /**
     * @param $str
     */
    public static function message($str)
    {
        $fname = self::getFname();
        self::createFile($fname);

        $time = microtime(true);
        $micro_seconds = sprintf('%03d',($time - (int)$time) * 1000);
        $prepared_msg = '[' . date('Y-m-d H:i:s.', $time) . $micro_seconds . '] ' . $str . PHP_EOL;
        file_put_contents($fname, $prepared_msg, FILE_APPEND);
    }

    /**
     * @return mixed
     */
    protected static function getFname()
    {
        return str_replace('<date>', date('Y-m-d'), self::$fname);
    }

    /**
     * @param $fname
     */
    protected static function createFile($fname)
    {
        if (!file_exists($fname)) {
            $dirname = dirname($fname);

            if (!file_exists($dirname)) {
                mkdir($dirname, 0777, true);
                chmod($dirname, 0777);
            }

            file_put_contents($fname, '', FILE_APPEND);
            chmod($fname, 0777);
        }
    }

}